﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//初始化棋盘
void InitBoard(char arr[ROWS][COLS], int rows, int cols,int ret)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = ret;
		}
		
	}
	
}
//打印棋盘
void DisPlayBpard(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	printf("----------扫雷游戏——————-————\n");
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		int j = 0;
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}

}


//布置雷
void SetMine(char arr[ROWS][COLS], int row, int col)
{
	int count = Set_mine;
	while (count)
	{
		int x = rand() % row + 1;     //创建1~9的随机数
		int y = rand() % col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}

//排查雷

int  GetMineCount(char mine[ROWS][COLS], int x, int y)
{ 
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		for (j = x - 1; j <= x + 1; j++)
		{
			count += (mine[i][j] - '0');

		}



	}

	return count;

}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win == row * col - Set_mine)
	{
		printf("请输入要排查的坐标\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				DisPlayBpard(mine, ROW, COL);
				break;
			}
			else 
			{
				int Add = GetMineCount(mine, x, y);
				show[x][y] = Add + '0';
				DisPlayBpard(show, ROW, COL);
				win++;
			}

		}
		else
		{

			printf("非法输入，请重新输入\n");
		}
		if (win == row * col - Set_mine)
		{
			printf("恭喜你，排雷成功\n");
			DisPlayBpard(mine, ROW, COL);
		}
	}
//
//
//
//}
//int GetMineCount(char mine[ROWS][COLS], int x, int y)
//{
//	return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x][y]+
//		mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0');
//}
//void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
//{
//	int x = 0;
//	int y = 0;
//	int win = 0;
//	while (win < row * col - Set_mine)
//	{
//		printf("请输⼊要排查的坐标:>");
//		scanf("%d %d", &x, &y);
//		if (x >= 1 && x <= row && y >= 1 && y <= col)
//		{
//			if (mine[x][y] == '1')
//			{
//				printf("很遗憾，你被炸死了\n");
//				DisplayBoard(mine, ROW, COL);
//				break;
//			}
//			else
//			{
//				//该位置不是雷，就统计这个坐标周围有⼏个雷
//				int count = GetMineCount(mine, x, y);
//				show[x][y] = count + '0';
//				DisPlayBoard(show, ROW, COL);
//				win++;
//			}
//		}
//		else
//		{
//			printf("坐标⾮法，重新输⼊\n");
//		}
//	}
//	if (win == row * col - Set_mine)
//	{
//		printf("恭喜你，排雷成功\n");
//		DisplayBoard(mine, ROW, COL);
//	}
//}